from distutils.core import setup
import py2exe
from py2exe.build_exe import py2exe as BuildExe
import os, sys

def TixInfo():
	import Tkinter
	import _tkinter

	tk = _tkinter.create()

	tcl_version = _tkinter.TCL_VERSION
	tk_version = _tkinter.TK_VERSION
	tix_version = tk.call("package", "version", "Tix")

	tcl_dir = tk.call("info", "library")

	del tk, _tkinter, Tkinter

	return (tcl_version, tk_version, tix_version, tcl_dir)

class myPy2Exe(BuildExe):

	def plat_finalize(self, modules, py_files, extensions, dlls):
		BuildExe.plat_finalize(self, modules, py_files, extensions, dlls)

		if "Tix" in modules:
			# Tix adjustments
			tcl_version, tk_version, tix_version, tcl_dir = TixInfo()

			tcl_src_dir = os.path.split(tcl_dir)[0]

			tixdll = "tix%s%s.dll" % (tix_version.replace(".", ""),
					tcl_version.replace(".", ""))
			tcldll = "tcl%s.dll" % tcl_version.replace(".", "")
			tkdll = "tk%s.dll" % tk_version.replace(".", "")

			tix_path = os.path.join(sys.prefix, "DLLs")
			if not os.path.isfile(os.path.join(tix_path, tixdll)):
				tixdll = "tix%s.dll" % tix_version.replace(".", "")
				tix_path = os.path.join(sys.prefix, tcl_src_dir, "tix%s" % tix_version)
				if not os.path.isfile(os.path.join(tix_path, tixdll)):
					print "Unable to find Tix dll"
					sys.exit(1)
			
			dlls.add(os.path.join(tix_path, tixdll))

			self.dlls_in_exedir.extend( [tcldll, tkdll, tixdll ] )

			tcl_dst_dir = os.path.join(self.lib_dir, "tcl")
			self.announce("Copying TIX files from %s..." % tcl_src_dir)
			self.copy_tree(os.path.join(tcl_src_dir, "tix%s" % tix_version),
					os.path.join(tcl_dst_dir, "tix%s" % tix_version))

options={"py2exe": {"compressed": 0, "optimize": 1, "bundle_files": 1} }

setup(options=options, zipfile=None, console=['yapget.py'])

setup(
	script_args=['py2exe'],
	zipfile=None,
	cmdclass={'py2exe': myPy2Exe},
	windows=['yapgui.pyw'],
	options=options
)
